<?php
namespace app\mouth\controller;
use app\mouth\common\OrderValidate;
use app\mouth\model\OrderModer;
use think\facade\Cache;
use think\Db;
use think\facade\Log;
use think\Collection;
/**
 *订单控制器
 */
class OrderApi extends Collection
{
    /**
     * 救援生成订单表
     *
     * 1  救援表
     * 2  充电表
     * 3  轮胎表
     */
    public function rescue(){
       $add = (new OrderModer)->rescue(input());
       if ( $add ){
          return json(['code' => 40006, 'message' => '成功', 'msg' => $add]);
       }
       return json(['code' => 40003, 'message' => '失败', 'msg' => []]);
    }

    /**
     * 修改救援订单状态
     * @param $table
     * @param $or_ordersn
     * @return array|false|mixed|\PDOStatement|string|\think\Model|null
     */
    public function trailerOrder($table,$or_ordersn){
       return (new OrderModer)->aggregate($table,['or_ordersn'=>$or_ordersn],['order_status'=>2],2);
    }

    /**
     * @return \think\response\Json
     * @return
     * full  商品价格
     * state1(洗车)2(保养)3(喷漆)4(轮胎)
     */
    public function pay()
    {
        $dates = Cache::get(input('openid'));
        $date = (new OrderModer)->manufacture($dates['openid'], input('state'), (int)input('full'), $dates['quanzi_id']['id']);
        if (is_string($date)) {
            return json(['code' => 40003, 'message' => '失败', 'msg' => '请先完善手机号']);
        } elseif (empty($date)) {
            return json(['code' => 40003, 'message' => '没有优惠券', 'msg' => $date]);
        }
        return json(['code' => 40006, 'message' => '成功', 'msg' => $date]);
    }

    /**
     * 生成订单--商品
     * @return \think\response\Json
     */
    public function indexpay()
    {
        $validate = new OrderValidate();
        if (!$validate->check(input())) {
            return json(['code' => 40003, 'message' => '失败', 'msg' => $validate->getError()]);
        }
        $date = Cache::get(input('openid'));
        $or_id = (new OrderModer)->indexpay(input(), $date['openid'], $date['nickname']);
        return json(['code' => 40006, 'message' => '成功', 'msg' => $or_id]);
    }

    /**
     * 调用支付
     */
    public function callpay(){
        input('boty') ? $body = input('boty') : $body = '商品支付';
       (new \app\mouth\controller\WxPay)->wxPay(input('total_fee'),(int)(input('out_trade_no')),'JSAPI',trim(input('openid')),$body);
    }

    /**
     * 支付失败
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function PaymentFailed(){
//        Db::name('order')->where('or_id',1)->update(['nickname'=>'田']);
    }

    /**
     * 支付成功
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function PaymentSuccessful(){
        Db::name('order')->where('or_id',1)->update(['nickname'=>'田']);
    }

    /**
     * 查看订单存不存在
     * @param $out_trade_no
     *
     * 订单号
     */
    public function viewOrder($table,$out_trade_no){
       return (new OrderModer)->aggregate($table,['or_ordersn'=>$out_trade_no],'ma_id',1);
    }

    /**
     * 更改订单状态
     * $out_trade_no
     * 订单号
     */
    public function changeStatus($out_trade_no,$openid){
        //更改优惠券
       $id = (new OrderModer)->aggregate('order',['or_ordersn'=>$out_trade_no],'u_di_id,od_goodsid',1);
       if (empty($u_di_id)){
           $dates = Cache::get($openid);
           $where = [
               'user_id'=>$dates['user_id']['id'],
               'volume_id'=>$id['u_di_id']
           ];
           (new OrderModer)->aggregate('wx_user_discount',$where,['date'=>2],3);
       }
       $value = ['vs_id'=>$id['od_goodsid']];
        (new OrderModer)->aggregate('yc_car_volume_store',$value,'vs_surplus',6);
        (new OrderModer)->aggregate('yc_car_volume_store',$value,'vs_amount',4);
       //更改订单状态
        return (new OrderModer)->aggregate('order',['or_ordersn'=>$out_trade_no],['or_status'=>2],2);
    }
    public function sale($maid){
        (new OrderModer)->aggregate('store',$maid,'volume',4);
    }

}